package com.ITArmy.controller.admin;

import com.ITArmy.beans.User;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.controller.base.BaseController;
import com.ITArmy.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Api(value = "后台用户接口层次", tags = "后台用户接口层次Controller层")
@Controller
@RequestMapping("/admin/User")
public class AdminUserController extends BaseController {

    @RequestMapping("/addUser")
    @ResponseBody
    @ApiOperation(value = "添加用户信息",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse addUser(User user) {
        user.setuTime(new Date());
        return userService.addUser(user);
    }

    @RequestMapping("/updateUser")
    @ApiOperation(value = "修改用户信息，", notes = "下面的所有的参数（uId不能修改）",
            httpMethod = "POST", response = ServerResponse.class)
    @ResponseBody
    public ServerResponse updateUser(User user) {
        return userService.updateUser(user, request);
    }

    @RequestMapping("/deleteUserByManager")
    @ApiOperation(value = "管理员删除用户信息", notes = "（单个删除）",
            httpMethod = "DELETE", response = ServerResponse.class)
    @ResponseBody
    public ServerResponse deleteUserByManager(Long uId) {
        return userService.deleteUserByManager(uId);
    }

    @RequestMapping("/deleteMany")
    @ApiOperation(value = "管理员删除用户信息", notes = "（批量删除）",
            httpMethod = "DELETE", response = ServerResponse.class)
    @ResponseBody
    public ServerResponse deleteMany(String ids) {
        String[] id = ids.split(",");
        return userService.deleteManyByIds(id);
    }

    @RequestMapping("/list")
    @ApiOperation(value = "后台遍历用户所有信息",
            httpMethod = "POST")
    public String list(@RequestParam(value = "page", defaultValue = "1") int page, Model model) {
        PageHelper.startPage(page, 9);
        List<User> users = userService.findAllUser();
        PageInfo PageInfo = new PageInfo<>(users, 9);
        model.addAttribute("userList", PageInfo);
        return "user";
    }

    @ApiOperation(value = "根据用户id查询用的用户的信息.",
            httpMethod = "POST", response = ServerResponse.class)
    @ResponseBody
    @RequestMapping("/findUserById")
    public ServerResponse findUserById(long uId) {
        return userService.findUserById(uId);
    }
}
